package com.googlecode.gaal.analysis.impl;

import java.util.Iterator;

import com.googlecode.gaal.analysis.api.VectorBuilder;
import com.googlecode.gaal.data.api.Corpus;

public class ConcurrentAligner<C> extends Aligner<C> {

    public ConcurrentAligner(final VectorBuilder<C> vectorBuilder, final Iterator<C> srcIterator,
            final Iterator<C> dstIterator, final Corpus<String> srcCorpus, final Corpus<String> dstCorpus,
            double minSimilarity, int alignmentsNumber, final int minVectorSize) {
        super(minSimilarity, alignmentsNumber, minVectorSize);
        Stopwatch stopwatch = null;
        if (Aligner.verbose) {
            stopwatch = new Stopwatch();
            stopwatch.start();
        }
        Runnable srcVectorBuilder = new Runnable() {

            @Override
            public void run() {
                vectorBuilder.buildVectors(srcIterator, srcVectors, srcObjects, srcCorpus, minVectorSize);
            }

        };

        Runnable dstVectorBuilder = new Runnable() {

            @Override
            public void run() {
                vectorBuilder.buildVectors(dstIterator, dstVectors, dstObjects, dstCorpus, minVectorSize);
            }
        };
        Thread srcThread = new Thread(srcVectorBuilder);
        srcThread.start();
        Thread dstThread = new Thread(dstVectorBuilder);
        dstThread.start();
        try {
            srcThread.join();
            dstThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (stopwatch != null) {
            stopwatch.stop();
            System.out.printf("vector construction took %s\n", stopwatch);
        }
    }
}
